import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'

/*引入Element-Plus组件库 并注册*/
import ElementPlus, { ElMessage } from 'element-plus'
import 'element-plus/dist/index.css'

/*全局引入reset.css 清除浏览器默认样式*/
import './assets/css/reset.css'

/*全局引入root.css 颜色变量文件 为之后前端做主题或其他效果做铺垫*/
import './assets/css/root.css'

/*全局引入element-plus的重置样式*/
import './assets/css/elementReset.css'

/*设置路由守卫*/
router.beforeEach((to, from, next) => {
    let userInfo = localStorage.getItem('userInfo'); // 使用 getItem 获取 localStorage 中的值
    if (to.path !== '/login' && !userInfo && to.path !== "/") {
        ElMessage.error("请先登录"); // 先显示错误消息
        next({ path: "/login", query: { redirect: to.fullPath } }); // 再跳转到登录页面
    } else {
        next();
    }
})

/*设置element-plus组件库为默认语言中文 并挂载*/
import zhCn from 'element-plus/es/locale/lang/zh-cn'

createApp(App).use(store).use(router).use(ElementPlus,{ locale: zhCn }).mount('#app')

// 配置根路径
const BASE_URL = 'http://localhost:8080';
window.BASE_URL = BASE_URL;

/*配置后端测试接口地址*/
const TEST_URL = 'http://localhost:8080';
window.TEST_URL = TEST_URL;

/*配置后端生产接口地址*/
const PRO_URL = 'http://www.xxx.com';
window.PRO_URL = PRO_URL;

//6.解决ResizeObserver Error
const debounce = (fn, delay) => {
    let timer = null;
    return function () {
        let context = this;
        let args = arguments;
        clearTimeout(timer);
        timer = setTimeout(function () {
            fn.apply(context, args);
        }, delay);
    }
}
const _ResizeObserver = window.ResizeObserver;
window.ResizeObserver = class ResizeObserver extends _ResizeObserver {
    constructor(callback) {
        callback = debounce(callback, 16);
        super(callback);
    }
}

//7.定义获取当前登录用户的全局方法
window.getUserInfo = ()=>{
    return localStorage.userInfo ? JSON.parse(localStorage.userInfo) : null;
}


